import * as maptalks from '~/maptalks/maptalks';
window.maptalks = maptalks;

maptalks.Map.prototype.hasLayer = function (layer) {
  for (var i = 0; i < this._layers.length; i++) {
    if (this._layers[i] == layer) {
      return true;
    }
  }
  return false;
}

maptalks.Map.prototype.clearLayers = function () {
  for (var i = this._layers.length; i > 0; i--) {
    if (this._layers[i - 1] instanceof maptalks.VectorLayer) {
      this.removeLayer(this._layers[i - 1]);
    }
  }
}

class JscMap {
  constructor(container, options) {
    this.handlers = {};

    this.options = Object.assign({
      center: [120, 39],
      maxPitch: 80,
      zoom: 16,
      maxZoom: 20,
      // cssFilter: 'invert(100%) saturate(90%) hue-rotate(180deg) contrast(0.75)'
    }, options);

    if (this.addEchartLayer) {
      this.addEchartLayer(container, options);
    }

    if (!this.map) {
      this.map = new maptalks.Map(container, options);
    }
    this.setBaseLayer(options.defaultMap);
  }

  setBaseLayer(type) {
    if (type == 'street' && this.options.streetUrl) {
      this.map.setBaseLayer(new maptalks.TileLayer('street', {
        urlTemplate: this.options.streetUrl,
        cssFilter: this.options.cssFilter
      }));
    } else if (type == 'satellite' && this.options.satelliteUrl) {
      this.map.setBaseLayer(new maptalks.TileLayer('satellite', {
        urlTemplate: this.options.satelliteUrl,
        cssFilter: this.options.cssFilter
      }));
      this.map.addLayer(new maptalks.TileLayer('satelliteLabel', {
        urlTemplate: this.options.satelliteLabelUrl
      }));
    } else if (type == 'vector' && this.options.vectorUrl && this.createVectorTile) {
      this.map.setBaseLayer(this.createVectorTile('vector', {
        style: this.options.vectorUrl
      }).layer);
    }
  }

  fitExtent(vectorLayer) {
    if (!this.map.hasLayer(vectorLayer)) return;
    var extent = vectorLayer.getExtent();
    if (extent != null) this.map.fitExtent(extent);
  }

  addListener(type, handler) {
    if (typeof this.handlers[type] == 'undefined') {
      this.handlers[type] = [];
    }
    this.handlers[type].push(handler);
  }

  removeListener(type, handler) {
    if (!this.handlers[type]) return;
    var handlers = this.handlers[type];
    if (handler == undefined) {
      handlers = [];
    } else if (handlers.length) {
      for (var i = 0; i < handlers.length; i++) {
        if (handlers[i] == handler) {
          this.handlers[type].splice(i, 1);
        }
      }
    }
  }

  fire(type, param) {
    if (this.handlers[type] instanceof Array) {
      var handlers = this.handlers[type];
      for (var i = 0; i < handlers.length; i++) {
        handlers[i](param);
      }
    }
  }

  recvDirect(e) {
    if (e.type == 'moveend' || e.type == 'zoomend') {
      this.map.animateTo({
        center: e.center,
        zoom: e.zoom,
      }, {
          duration: 1000
        });
    }
    else if (e.type == 'pitchend') {
      this.map.setPitch(e.pitch);
    }
    else if (e.type == 'rotateend') {
      this.map.setBearing(e.bearing);
    }
    else if (e.type == 'click') {

    }
  }

}

JscMap.include = function (props) {
  for (var i in props) {
    this.prototype[i] = props[i];
  }
};

export default JscMap;